<!-- templates/field_list.html -->
{% extends 'base.html' %}

{% block title %}{{ data_source.name }} - 字段管理{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-start">
    <div class="col-12 col-md-8 col-lg-10">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3">{{ table.name }} - 字段管理</h1>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createFieldModal">
            <i class="fa fa-plus"></i> 新建字段
        </button>
    </div>
    
    <div class="mb-4">
        <a href="{% url 'table_list' datasource_pk=data_source.pk %}" class="btn btn-sm btn-outline-secondary">
            <i class="fa fa-arrow-left"></i> 返回数据表列表
        </a>
    </div>
    <!-- 搜索框 -->
        <div class="mb-3 d-flex">
            <input type="text" id="datasourceSearchInput" class="form-control me-2" placeholder="搜索字段名称/描述...">
            <button id="datasourceSearchBtn" class="btn btn-outline-secondary" type="button">
                <i class="fa fa-search"></i> 
            </button>
        </div>
    
    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                {{ message }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endfor %}
    {% endif %}
    
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th class="px-3 text-center" >名称</th>
                            <th class="px-3 text-center">类型</th>
                            <th class="px-3 text-center">描述</th>
                            <th class="px-3 text-center">是否必填</th>
                            <th class="px-3 text-center">创建时间</th>
                            <th class="px-3 text-center">更新时间</th>
                            <th class="px-3 text-center">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for field in fields %}
                        <tr >
                            <td class="px-3 text-center">{{ field.name }}</td>
                            <td>
                                {% if field.field_type == 'string' %}
                                    <span class="badge bg-primary">字符串</span>
                                {% elif field.field_type == 'integer' %}
                                    <span class="badge bg-success">整数</span>
                                {% elif field.field_type == 'float' %}
                                    <span class="badge bg-info">浮点数</span>
                                {% elif field.field_type == 'boolean' %}
                                    <span class="badge bg-purple">布尔值</span>
                                {% elif field.field_type == 'date' %}
                                    <span class="badge bg-warning">日期</span>
                                {% elif field.field_type == 'datetime' %}
                                    <span class="badge bg-warning">日期时间</span>
                                {% elif field.field_type == 'text' %}
                                    <span class="badge bg-secondary">文本</span>
                                {% endif %}
                            </td>
                            <td class="px-3 text-center">{{ field.description|truncatechars:50 }}</td>
                            <td class="px-3 text-center">
                                {% if field.is_required %}
                                    <i class="fa fa-check text-success"></i>
                                {% else %}
                                    <i class="fa fa-times text-muted"></i>
                                {% endif %}
                            </td>
                            <td class="px-3 text-center">{{ field.created_at|date:"Y-m-d H:i" }}</td>
                            <td class="px-3 text-center">{{ field.updated_at|date:"Y-m-d H:i" }}</td>
                            <td class="px-3 text-center">
                                <a href="{% url 'field_list' datasource_pk=data_source.pk table_pk=table.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fa fa-edit"></i> 编辑
                                </a>
                                <a href="{% url 'field_list' datasource_pk=data_source.pk table_pk=table.pk %}" class="btn btn-sm btn-outline-danger">
                                    <i class="fa fa-trash"></i> 删除
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fa fa-folder-open-o fa-3x mb-2"></i>
                                <p>暂无字段，请创建一个新的字段。</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</div>
</div>

<!-- 创建数据源模态框 -->
<div class="modal fade" id="createFieldModal" tabindex="-1" aria-labelledby="createFieldModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <form method="post" id="createFieldModalForm">
                {% csrf_token %}
                <div class="modal-header">
                    <h5 class="modal-title" id="createFieldModalLabel">创建字段</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <!-- 字段名称 -->
                    <div class="mb-3">
                        <label for="id_name" class="form-label">名称<span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="id_name" name="name" required>
                    </div>
                    <!-- 字段类型 -->
                    <div class="mb-3">
                        <label for="id_field_type" class="form-label">类型<span class="text-danger">*</span></label>
                        <select class="form-select" id="id_field_type" name="field_type" required>
                            <option value="">请选择</option>
                            <option value="string">字符串</option>
                            <option value="integer">整数</option>
                            <option value="float">浮点数</option>
                            <option value="boolean">布尔值</option>
                            <option value="date">日期</option>
                            <option value="datetime">日期时间</option>
                            <option value="text">文本</option>
                        </select>
                    </div>
                    <!-- 描述 -->
                    <div class="mb-3">
                        <label for="id_description" class="form-label">描述</label>
                        <textarea class="form-control" id="id_description" name="description" rows="3"></textarea>
                    </div>
                    <!-- 是否必填 -->
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="id_is_required" name="is_required">
                        <label class="form-check-label" for="id_is_required">必填</label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        <i class="fa fa-times mr-1"></i>取消
                    </button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-check mr-1"></i>保存
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}    